package org.oliver.crp.samtrack.client;

import org.oliver.crp.samtrack.client.ui.data.EditNavigationPaneSectionData;
import org.oliver.crp.samtrack.client.ui.data.NavigationPaneRecord;
import org.oliver.crp.samtrack.client.ui.data.ReportNavigationPaneSectionData;
import org.oliver.crp.samtrack.client.ui.data.SampleNavigationPaneSectionData;
import org.oliver.crp.samtrack.client.ui.data.administrationNavigationPaneSectionData;
import org.oliver.crp.samtrack.client.ui.view.ContextAreaFactory;
import org.oliver.crp.samtrack.client.ui.view.SampleView;
import org.oliver.crp.samtrack.client.ui.widgets.ApplicationMenu;
import org.oliver.crp.samtrack.client.ui.widgets.Masthead;
import org.oliver.crp.samtrack.client.ui.widgets.NavigationPane;
import org.oliver.crp.samtrack.client.ui.widgets.NavigationPaneHeader;
import org.oliver.crp.samtrack.shared.AppUserProxy;
import org.oliver.crp.samtrack.shared.AppUserRequest;
import org.oliver.crp.samtrack.shared.ApplicationRequestFactory;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.resources.client.CssResource.NotStrict;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.web.bindery.event.shared.EventBus;
import com.google.web.bindery.event.shared.SimpleEventBus;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.grid.events.RecordClickEvent;
import com.smartgwt.client.widgets.grid.events.RecordClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.menu.events.ClickHandler;
import com.smartgwt.client.widgets.menu.events.MenuItemClickEvent;

import java.util.Date;
//import com.google.appengine.api.datastore.DatastoreService;
//import com.google.appengine.api.datastore.DatastoreServiceFactory;
//import com.google.appengine.api.datastore.Entity;


/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Samtrack implements EntryPoint {
	
//	TestPersObjRequestFactory requestFactory;
	
	private static final int NORTH_HEIGHT = 85; // MASTHEAD_HEIGHT + APPLICATION_MENU_HEIGHT
	private static final int DEFAULT_MENU_WIDTH = 70;	
	
	private VLayout mainLayout;	
	private HLayout northLayout;  
	private HLayout southLayout;
	private VLayout eastLayout;  
	private VLayout westLayout;
	
	NavigationPaneHeader navigationPaneHeader;
	
	ApplicationMenu applicationMenu;
	
	
	final EventBus eventBus = new SimpleEventBus();
	ApplicationRequestFactory requestFactory;
	
	interface GlobalResources extends ClientBundle {
		@NotStrict
		@Source("Samtrack.css")
		CssResource css();
	}
	  
	public void onModuleLoad() {
		
//		final EventBus eventBus = new SimpleEventBus();
//		requestFactory = GWT.create(TestPersObjRequestFactory.class);
//		requestFactory.initialize(eventBus);
		
		GWT.log("init OnLoadModule()...", null);
		
		// inject global styles
		GWT.<GlobalResources>create(GlobalResources.class).css().ensureInjected(); 

		Window.enableScrolling(false);
		Window.setMargin("0px");
		
		mainLayout = new VLayout();
		mainLayout.setWidth100();
		mainLayout.setHeight100();
		
		navigationPaneHeader = new NavigationPaneHeader();
		
	    // initialise the North layout container
	    northLayout = new HLayout();  
	    northLayout.setHeight(NORTH_HEIGHT); 

	    VLayout vLayout = new VLayout(); 
	    // add the Masthead to the nested layout container
	    vLayout.addMember(new Masthead());
	    
	    applicationMenu = new ApplicationMenu();
	    applicationMenu.addMenu("<u>F</u>ile", DEFAULT_MENU_WIDTH, "Task, Fax", new ApplicationMenuClickHandler());
	    applicationMenu.addMenu("<u>E</u>dit", DEFAULT_MENU_WIDTH,
	                            "Account, Contact, separator, Lead, Opportunity", new ApplicationMenuClickHandler());
	    applicationMenu.addMenu("<u>S</u>earch", DEFAULT_MENU_WIDTH,
                "Account, Contact, separator, Lead, Opportunity", new ApplicationMenuClickHandler());
	    applicationMenu.addMenu("<u>W</u>orkflow", DEFAULT_MENU_WIDTH,
                "Account, Contact, separator, Lead, Opportunity", new ApplicationMenuClickHandler());
	    applicationMenu.addMenu("<u>H</u>elp", DEFAULT_MENU_WIDTH,
                "Account, Contact, separator, Lead, Opportunity", new ApplicationMenuClickHandler());
	    // add the Application menu to the nested layout container
	    vLayout.addMember(applicationMenu);
	    vLayout.addMember(navigationPaneHeader);
	    // add the nested layout container to the  North layout container
	    northLayout.addMember(vLayout);

	    // initialise the East layout container
//	    eastLayout = new ContextArea(); 
	    
	    NavigationPane navigationPane = new NavigationPane();
	    navigationPane.add("Samples", SampleNavigationPaneSectionData.getRecords(), new NavigationPaneClickHandler());
	    navigationPane.add("Edit", EditNavigationPaneSectionData.getRecords(), new NavigationPaneClickHandler());
	    navigationPane.add("Report", ReportNavigationPaneSectionData.getRecords(), new NavigationPaneClickHandler());
	    navigationPane.add("Administration", administrationNavigationPaneSectionData.getRecords(), new NavigationPaneClickHandler());
	    
	    navigationPane.expandSection(0);
	    
	    westLayout = navigationPane;
	    
	    eastLayout = new SampleView();
	    
	    // initialise the South layout container
	    southLayout = new HLayout(); 
	 
	    // set the Navigation Pane and ContextArea as members of the South 
	    // layout container 
	    southLayout.setMembers(westLayout, eastLayout);  
	    
	    // add the North and South layout containers to the main layout container
	    mainLayout.addMember(northLayout);  
	    mainLayout.addMember(southLayout); 
	    
	    RootLayoutPanel.get().add(mainLayout);
	    
	    bind();
	}
	
	private class ApplicationMenuClickHandler implements ClickHandler {

		@Override
		public void onClick(MenuItemClickEvent event) {
			String appName = event.getItem().getTitle();
			SC.say("You clicked: ", appName);
		}		
	}
	
	private class NavigationPaneClickHandler implements RecordClickHandler {

		@Override
		public void onRecordClick(RecordClickEvent event) {
			NavigationPaneRecord record = (NavigationPaneRecord) event.getRecord();
//			event.getRecord()event;
			String tmp = record.getAttribute("name");
			
			if (record.getAttribute("name").equals("Add Person Test")) {

//				putTestEntry();
				
				
				
				
				AppUserRequest userRequest = requestFactory.appUserRequest();
				
				AppUserProxy appUserProxy = userRequest.create(AppUserProxy.class);
				appUserProxy.setName("Hans");
				userRequest.persist().using(appUserProxy).fire();
			}
			
			setContextAreaView(record);
		}
		
		private void setContextAreaView (NavigationPaneRecord record) {
			
			// "Activities" or "Calendar", etc.
			String name = record.getName();
			navigationPaneHeader.setContextAreaHeaderLabelContents(name);
			
			ContextAreaFactory factory = record.getFactory();
			Canvas view = factory.create();
			southLayout.setMembers(westLayout, view);
		}
	}
	
	private void putTestEntry() {
//		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
//
//		Entity employee = new Entity("Employee");
//		employee.setProperty("firstName", "Antonio");
//		employee.setProperty("lastName", "Salieri");
//		Date hireDate = new Date();
//		employee.setProperty("hireDate", hireDate);
//		employee.setProperty("attendedHrTraining", true);
//
//		datastore.put(employee);
	}
	
	private void bind() {
		requestFactory = GWT.create(ApplicationRequestFactory.class);
		requestFactory.initialize(eventBus);
	}
}
